package add__8_HashMap;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;


public class TestMap {

    public static void main(String[] args) {
        Map<String, Integer> map1 = new HashMap<String, Integer>();
        map1.put("小明", 90);
        map1.put("小明", 20);
        System.out.println(map1);
        System.out.println(map1.get("小明"));
        
        
        System.out.println("------------------------------------------");
        
        // HashMap去重功能是依赖元素的 hashCode 和 equals 方法判断的，自定义类型记得重写hashCode和equals方法
        Map<IndexIJ, Boolean> map2 = new HashMap<IndexIJ, Boolean>();
        map2.put(new IndexIJ(1, 1), false);
        map2.put(new IndexIJ(1, 1), true);
        map2.put(new IndexIJ(1, 4), false);
        map2.put(new IndexIJ(2, 2), false);
        System.out.println(map2);
        System.out.println(map2.get(new IndexIJ(1, 1)));
    }

}
class IndexIJ {
    public int i, j;
    public IndexIJ(int i, int j) {
        this.i = i;
        this.j = j;
    }
    @Override
    public String toString() {       
        return "(" + i + ", " + j + ")";
    }
    @Override
    public int hashCode() {
        return Objects.hash(i, j);
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        IndexIJ other = (IndexIJ) obj;
        return i == other.i && j == other.j;
    }
    
}